<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>封装简易ajax函数</title>
</head>
<body>
  <script>
    function resolveData(data) {
      var arr = []
      for (var k in data) {
        var str = k + '=' + data[k]
        arr.push(str)
      }
      return arr.join('&')
    }
    // var res = resolveData({name:'zs',age :20})
    // console.log(res);

    function ajax(options) {
      var xhr = new XMLHttpRequest()
      //把外界转递过来的参数对象，转换为查询字符串
      var qs = resolveData(options.data)
      if (options.method.toUpperCase() === 'GET') {
        //发起GET请求
        xhr.open(options.method, options.url + '?' + qs)
        xhr.send()
      } else if (options.method.toUpperCase() === 'POST') {
        //发起POST请求
        xhr.open(options.method, options.url)
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        xhr.send(qs)
      }
      xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
          var result = JSON.parse(xhr.responseText)
          options.success(result)
        }
      }
    }
  </script>
</body>
</html>